<?php
/**
 * News is an instance of CActiveRecord (sytem.db.ar). 
 *
 * CActiveRecord is the base class for classes representing relational data. 
 *
 * Eduow :  International system for education (http://www.eduow.com)
 * Copyright 2010, Nikolay Stoitsev
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.models
 * @inheritance   class News >> CActiveRecord >> CModel >> CComponent
 * @implements    ArrayAccess, Traversable, IteratorAggregate
 * @since         Eduow v 0.0.4
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */

class News extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'news':
	 * @var integer $id
	 * @var string $title
	 * @var string $content
	 * @var integer $create_time
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName(){
		return 'news';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules(){
		return array(
			array('title, content', 'required'),
			array('create_time', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>150),
			array('language','safe'),
			array('id, title, content, create_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * Returns the default named scope that should be implicitly applied to all queries for this model.
	 */
	public function defaultScope(){
		return array(
    			'condition'=>'language="'.Yii::app()->language.'"',
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels(){
		return array(
			'id' => Yii::t('yii','Id'),
			'title' => Yii::t('yii','Title'),
			'content' => Yii::t('yii','Content'),
			'create_time' => Yii::t('yii','Create Time'),
		);
	}

	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->create_time=time();
			}
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search(){

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('title',$this->title,true);

		$criteria->compare('content',$this->content,true);

		$criteria->compare('create_time',$this->create_time);

		return new CActiveDataProvider('News', array(
			'criteria'=>$criteria,
		));
	}
}
